package com.a16.yuejian.yuejian.model;

import com.a16.yuejian.yuejian.javabean.Comment;
import com.a16.yuejian.yuejian.javabean.Post;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.Listener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.service.PostService;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 王子恒 on 2017/10/25.
 */

public class PostModel {
    private Retrofit retrofit;
    public PostModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:8080/neu_yuejian201/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    public void allpost(final Listener listener){
        PostService postService=retrofit.create(PostService.class);
        Call<List<Post>> call=postService.allpost();
        call.enqueue(new Callback<List<Post>>() {
            @Override
            public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<List<Post>> call, Throwable t) {

            }
        });
    }

    public void getcomment(final Listener listener , String postid){
        PostService postService=retrofit.create(PostService.class);
        Call<List<Comment>> call=postService.getcomment(postid);
        call.enqueue(new Callback<List<Comment>>() {
            @Override
            public void onResponse(Call<List<Comment>> call, Response<List<Comment>> response) {
                listener.onResponse(response.body());
            }


            @Override
            public void onFailure(Call<List<Comment>> call, Throwable t) {

            }
        });
    }
    public void comment(String userid,String content,String postid,final SuccessListener listener){
        PostService postService = retrofit.create(PostService.class);
        Call<Success> call = postService.comment(userid,content,postid);
        call.enqueue(new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<Success> call, Throwable t) {

            }
        });
    }
    public void getuserpost(String userid ,final Listener listener){
        PostService postService = retrofit.create(PostService.class);
        Call<List<Post>> call = postService.getuserpost(userid);
        call.enqueue(new Callback<List<Post>>() {
            @Override
            public void onResponse(Call<List<Post>> call, Response<List<Post>> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<List<Post>> call, Throwable t) {

            }
        });
    }
    public void send(String userid,String content,String filename,final SuccessListener listener){
        PostService postService = retrofit.create(PostService.class);
        final File file = new File(filename);
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("pic", file.getName(),requestBody);
        Call<Success> call = postService.send(userid,content,body);
        call.enqueue(new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                listener.onResponse(response.body());
            }

            @Override
            public void onFailure(Call<Success> call, Throwable t) {

            }
        });
    }

}
